<?php
defined('IN_YuLin') || exit('NO PERMIT!');
UserLogin(U($m.'/'.$c.'/'.$a));

$cateTab   = Table('lottery_category');
$sql       = 'SELECT id, name FROM ' . $cateTab . ' WHERE status = 0';
$cateList  = $db->getall($sql, 'id');
// 搜索内容
$user_type  = NoBadStr($_GET['usertype']);
$user_name  = NoBadStr($_GET['username']);
$infoTab    = Table('user_info');
$userTab    = Table('user');
$invTab     = Table('user_invita_code');
// 获取属于用户的玩家信息
$invCodeTab = Table('user_invita_code');
$sql        = 'SELECT id FROM ' . $invCodeTab . ' WHERE uid = ' . $_uid;
$codeIdList = $db->getall($sql);
$codeIdStr  = ' AND invita_code_id IN(0';
foreach ($codeIdList as $k => $v){
    $codeIdStr .= ', ' . $v['id'];
}
$codeIdStr .= ')';
// 查询当前代理下的所有邀请码的注册用户
$userLineData  = array();
getUserLine($_uid, $userLineData);
// 把这些用户的id组合起来准备查询
$uidStr   = ' AND u.id IN(';
$uidStr  .= implode(',', array_keys($userLineData));
$uidStr  .= ')';
// 查询条件
if ($user_name){
    $uidStr .= ' AND u.username = "' . $user_name . '"';
}
if ($user_type){
    $uidStr .= ' AND info.user_type = "' . $user_type . '"';
}
$sql   = 'SELECT count(u.id) as p FROM ' . $userTab . ' AS u
            LEFT JOIN ' . $infoTab . ' AS info
            ON u.`id` = info.`uid` 
            LEFT JOIN ' . $invTab . ' AS invcd
            ON info.`invita_code_id` = invcd.id
            WHERE 1 ' . $uidStr;

// 查询当前代理下的玩家
$data  = [];
$tmp = $db->getrow($sql);
if($tmp['p'] > 0){
    $sql = '
            SELECT u.id, u.username, info.user_type, lower_nums, u.money, u.last_time, invcd.`uid` AS inv_uid, invcd.`id` as inv_id FROM ' . $userTab . ' AS u
            LEFT JOIN ' . $infoTab . ' AS info
            ON u.`id` = info.`uid` 
            LEFT JOIN ' . $invTab . ' AS invcd
            ON info.`invita_code_id` = invcd.id
            WHERE 1 ' . $uidStr;
    $page = max(1,intval($_GET['page']));
    $size = 20;
    $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
    $url = '?m='.$m.'&c='.$c;
    $orderby = ' ORDER BY u.id DESC ';
    $data = $db->getall($sql . $orderby.$limit);
    $multi = Multi($url,$page,$tmp['p'],$size);
}
$tpl->display('member/agentMember');
?>